﻿Public Class ObjectNodeVm
    Inherits NodeVm
    Implements IObjectChangeListener

    Public Sub New(obj As RawObject, typeNode As TypeNodeVm, objectTree As ObjectTree)
        MyBase.New(objectTree)

        RawObject = obj
        Parent = typeNode
        Me.ObjectType = obj.ObjectType
        Me.ObjectSubType = obj.ObjectSubType

        ObjectChangeManager.Instance.RegisterForChangeNotice(RawObject, Me)
    End Sub

    Private Parent As TypeNodeVm
    Public Property RawObject As RawObject

    Protected Overrides Sub OnIsSelected()
        MyBase.OnIsSelected()

        ObjectTree.SelectedObject = Me.RawObject
    End Sub

    'Public ReadOnly Property Image As System.Drawing.Image
    '    Get
    '        Return ObjectTree.Library.GetImageForObject(RawObject, AppData.Config)
    '    End Get
    'End Property

    Protected Overrides Sub BuildContextMenuInternal(list As List(Of MenuInfo))
        Dim command As Command = Nothing


        list.Add(New MenuInfo("Cut", "/Icons/CutHS.png", command.NotImplementedCommand))
        list.Add(New MenuInfo("Copy", "/Icons/CopyHS.png", command.NotImplementedCommand))
        list.Add(New MenuInfo("Paste", "/Icons/PasteHS.png", command.NotImplementedCommand))
        list.Add(New MenuInfo("Delete", "/Icons/DeleteHS.png", command.NotImplementedCommand))
        list.Add(Nothing)
        list.Add(New MenuInfo("Edit Raw Data", Nothing, New Command(AddressOf EditRawData)))
        list.Add(New MenuInfo("Popout", Nothing, New Command(AddressOf PopoutEditor)))
        list.Add(New MenuInfo("New " & Replace(RawObject.ObjectType.ToString, "_", "__"), "/Icons/AddObject.png", command.NotImplementedCommand))
    End Sub

    Private Sub EditRawData(param As Object)
        MessageService.Instance.BroadcastMessage(New Messages.MainWindow.OpenRawView With {.obj = RawObject})
    End Sub

    Private Sub PopoutEditor(param As Object)
        MessageService.Instance.BroadcastMessage(New Messages.MainWindow.PopoutEditor With {.obj = RawObject})
    End Sub

    Private Sub ObjectChanged(e As ObjectChangeEventArgs) Implements IObjectChangeListener.ObjectChanged
        RawObjectToImageConverter.Decache(e.Obj)
        If e.Obj Is RawObject AndAlso e.Source IsNot Me Then
            RaisePropertyChanged("")
        End If
    End Sub
End Class